<?php
define('AJAX_SCRIPT', true);
require_once('../../config.php');
require_once($CFG->dirroot.'/local/myplugin/lib.php');
require_once('./lib.php');


global $DB;
global $USER;
global $SESSION;

$tim = required_param('tim', PARAM_TEXT);
$survey_id = required_param('survey_id', PARAM_TEXT);
$status_id = required_param('status_id', PARAM_TEXT);
$emailstring=required_param('emailstring', PARAM_TEXT);

if($tim=='showstatisticsurvey')
{
    $curLang = $SESSION->lang;
      $str_survey="";
			$str_survey= $str_survey.'<table class=generalbox border=0 align=center style="margin-left:16%;">';
            $string ='';
            $surveys = $DB->get_records('itu_survey', array('id'=>$survey_id));
            $program_id='';
            foreach($surveys as $survey){
            $program_id = $survey->itus_program;
            }
            $programname ='';
            $program_names = $DB->get_records('program', array('id'=>$program_id));
            foreach($program_names as $program_name)            
            $programname = $program_name->program_name;
            
            $string = $string.' '.$programname;
            $string= get_string('survey','local_itusurveyplugin').'- '.$string.' '.get_string('program','local_itusurveyplugin');
            
            $majors = $DB->get_records('surveyed_majors',array('sm_survey'=>$survey_id,'sm_program'=>$program_id));
            foreach($majors as $major)
            {
                $string = $string.' - '.$major->sm_name;
            }
            //English
   
            $str_survey= $str_survey.'<tr><td>'.'<div><b>'.get_string('statisticheading','local_itusurveyplugin').'</b></div></td>';
            if($curLang == 'en'|| $curLang==NULL)
            $str_survey= $str_survey.'<td> '.$string;
            
            // vietnamese
            $string= get_string('survey','local_itusurveyplugin').'- '.get_string('program','local_itusurveyplugin');
            $surveys = $DB->get_records('itu_survey', array('id'=>$survey_id));
            $program_id='';
            foreach($surveys as $survey){
            $program_id = $survey->itus_program;
            }
            $programname ='';
            $program_names = $DB->get_records('program', array('id'=>$program_id));
            foreach($program_names as $program_name)            
            $programname = $program_name->program_name;
            
            $string = $string.' '.$programname;
            $majors = $DB->get_records('surveyed_majors',array('sm_survey'=>$survey_id,'sm_program'=>$program_id));
            foreach($majors as $major)
            {
                $string = $string.' - '.$major->sm_name;
            }
            if($curLang == 'vi')
            $str_survey= $str_survey.'<td> '.$string;
            
            $str_survey= $str_survey.'</td>';
            $str_survey= $str_survey.'</tr>';
            
            $str_survey= $str_survey.'<tr><td>'.'<div><b>'.get_string('program','local_itusurveyplugin').'</b></div></td>';
            $str_survey= $str_survey.'<td>'.$programname.'</td>';
            $str_survey= $str_survey.'</tr>';
            
            $string='';
            $majors = $DB->get_records('surveyed_majors',array('sm_survey'=>$survey_id,'sm_program'=>$program_id));
            foreach($majors as $major)
            {
                $string = $string.' - '.$major->sm_name.'</br>';
            }
            $str_survey= $str_survey.'<tr><td>'.'<div><b>'.get_string('major','local_itusurveyplugin').'</b></div></td>';
            $str_survey= $str_survey.'<td>'.$string.'</td>';
            $str_survey= $str_survey.'</tr>';
            
            $date='';
            $surveys = $DB->get_records('itu_survey', array('id'=>$survey_id));
                    foreach($surveys as $survey){
            $date = $survey->itus_expire_date;
            }
                

            $str_survey= $str_survey.'<tr><td>'.'<div><b>'.get_string('expire_date','local_itusurveyplugin').'</b></div></td>';
            if($curLang == 'en'|| $curLang==NULL)
            {
                $year = substr($date,0,4);
                $month = substr($date,5,2);
                $day = substr($date,8,2);
            if($month == '01')
            $month = 'January';
            if($month == '02')
            $month = 'February';
            if($month == '03')
            $month = 'March';
            if($month == '04')
            $month = 'April';
            if($month == '05')
            $month = 'May';
            if($month == '06')
            $month = 'June';
            if($month == '07')
            $month = 'July';
            if($month == '08')
            $month = 'August';
            if($month == '09')
            $month = 'September';
            if($month == '10')
            $month = 'October';
            if($month == '11')
            $month = 'November';
            if($month == '12')
            $month = 'December';
            $date = $month.' '.$day.' '.$year;
            }
            else if($curLang='vi')
            {
                $year = substr($date,0,4);
                $month = substr($date,5,2);
                $day = substr($date,8,2);
                        if($month == '01')
            $month = 'January';
            if($month == '02')
            $month = 'February';
            if($month == '03')
            $month = 'March';
            if($month == '04')
            $month = 'April';
            if($month == '05')
            $month = 'May';
            if($month == '06')
            $month = 'June';
            if($month == '07')
            $month = 'July';
            if($month == '08')
            $month = 'August';
            if($month == '09')
            $month = 'September';
            if($month == '10')
            $month = 'October';
            if($month == '11')
            $month = 'November';
            if($month == '12')
            $month = 'December';
            $date = $day.' '.$month.' '.$year;
            }
            $str_survey= $str_survey.'<td>'.$date.'</td>';
            $str_survey= $str_survey.'</tr>';
            
            $str_survey= $str_survey.'</table>';
            
            // table 2
            
			$str_survey=$str_survey.'<table class=generalbox border=1 align=center style="margin-left:16%;">'.
			'<th class="headingblock header">'.get_string('totalteachers','local_itusurveyplugin').'</th>'.
			'<th class="headingblock header">'.get_string('teachersdoing','local_itusurveyplugin').'</th>'.
            '<th class="headingblock header">'.get_string('teachersdid','local_itusurveyplugin').'</th>'.
            '<th class="headingblock header">'.get_string('teachersnotdo','local_itusurveyplugin').'</th>';
            
            $totalteachers=0;
            $teachersdoing=0;
            $teachersdid=0;
            $teachersnotdo =0;
            $teachers = $DB->get_records('itus_teachers_subjects',array('sts_survey_id'=>$survey_id));
            foreach($teachers as $teacher){
            if($teacher->sts_status == 0)
            $teachersnotdo++;
            if($teacher->sts_status == 1)
            $teachersdoing++;
            if($teacher->sts_status == 2)
            $teachersdid++;
            $totalteachers++;
            }
            
            $str_survey=$str_survey.'<tr style="cursor:pointer;"><td>'.$totalteachers.'</td>';
            $str_survey=$str_survey.'<td>'.$teachersdoing.'</td>';
            $str_survey=$str_survey.'<td>'.$teachersdid.'</td>';
            $str_survey=$str_survey.'<td>'.$teachersnotdo.'</td>';
            $str_survey=$str_survey.'</tr>';
            $str_survey=$str_survey.'</table>';
            
            
    echo $str_survey;
}
else if($tim =='showstatisticbystatus')
{
    $curLang = $SESSION->lang;
     $email_teacher_string='';
    $subject_teacher_string ='';
    $str_survey ='';
    /////////////////////////////////////////////
            if($status_id=='0'){    
            //table 3
			$str_survey=$str_survey.'<table class=generalbox border=1 align=center style="margin-left:16%;">'.
			'<th class="headingblock header">'.get_string('subject','local_itusurveyplugin').'</th>'.
			'<th class="headingblock header">'.get_string('teachername','local_itusurveyplugin').'</th>'.
            '<th class="headingblock header">'.get_string('status','local_itusurveyplugin').'</th>'.
            //'<th class="headingblock header">'.get_string('remind','local_itusurveyplugin').'</th>'.
            '<th class="headingblock header">'.get_string('email','local_itusurveyplugin').' '.'<input type="checkbox" id="checkall" onclick="checkallemail($(\'#string_teacher\').val(),$(\'#string_email\').val())"'.'</th>';
            
            $subjects = $DB->get_records('itus_teachers_subjects', array('sts_survey_id'=>$survey_id));
            
            $subject_id = array();
            $teacher_id = array();
            $i=0;
            foreach($subjects as $subject)
            {
                $subject_n='';
                if($i==0){
                //Subject
                $subject_id[$i]= $subject->sts_subject_id;
                $subject_names = $DB->get_records('subject',array('id'=>$subject_id[$i]));
                
                if($curLang=='en'|| $curLang==NULL){
                foreach($subject_names as $subject_name)
                $subject_n = $subject_name->subject_name_en;
                }
                
                if($curLang=='vi'){
                foreach($subject_names as $subject_name)
                $subject_n = $subject_name->subject_name_vn;
                } 
                if($subject_n =='')
                $subject_n = $subject_name->subject_name_vn;
                $str_survey=$str_survey.'<tr style="cursor:pointer;"><td>'.$subject_n.'</td>';
                
                //Teacher
                $str_survey=$str_survey.'<td>';
                $str_survey=$str_survey.'<table>';
                $teachers = $DB->get_records('itus_teachers_subjects',array('sts_subject_id'=>$subject_id[$i],'sts_survey_id'=>$survey_id));
                $n=0;
                foreach($teachers as $teacher){
                    $teacher_id[$n] = $teacher->sts_teacher_id;
                    
                    $teachername = $DB->get_record('user',array('id'=>$teacher_id[$n]));
                    $teacher_name = $teachername->firstname.' '.$teachername->lastname;
                    $str_survey=$str_survey.'<tr><td>'.$teacher_name.'</td></tr>';
                    
                    $n++;
                }
                $str_survey=$str_survey.'</table>';
                $str_survey=$str_survey.'</td>';
                
                
                //Status
                $str_survey=$str_survey.'<td>';
                $str_survey=$str_survey.'<table>';
                $teachers = $DB->get_records('itus_teachers_subjects',array('sts_subject_id'=>$subject_id[$i],'sts_survey_id'=>$survey_id));
                foreach($teachers as $teacher){
                    if($teacher->sts_status ==0)
                    $str_survey=$str_survey.'<tr><td>'.get_string('notdo','local_itusurveyplugin').'</td></tr>';
                    
                    if($teacher->sts_status ==1)
                    $str_survey=$str_survey.'<tr><td>'.get_string('doing','local_itusurveyplugin').'</td></tr>';
                    
                    if($teacher->sts_status ==2)
                    $str_survey=$str_survey.'<tr><td>'.get_string('did','local_itusurveyplugin').'</td></tr>';
                }
                    
                 $str_survey=$str_survey.'</table>';
                $str_survey=$str_survey.'</td>';
                
                // Remind vs email
                $str_survey=$str_survey.'<td>';
                
                $str_survey=$str_survey.'<form action="../itusurveyreport/email.php" method="POST">';
                $str_survey=$str_survey.'<table>';
                $teachers = $DB->get_records('itus_teachers_subjects',array('sts_subject_id'=>$subject_id[$i],'sts_survey_id'=>$survey_id));
              foreach($teachers as $teacher){
                    if($teacher->sts_status ==0)
                    {
                    $subject_teacher_string = $subject_teacher_string.','.$teacher->sts_subject_id.'_'.$teacher->sts_teacher_id.',';
                    $email_teacher = $DB->get_record('user',array('id'=>$teacher->sts_teacher_id));
                    $email_teacher_string=$email_teacher_string.','.$email_teacher->email.'//'.$teacher->sts_subject_id.',';
                    $str_survey=$str_survey.'<tr><td>'.'<input type="checkbox" name="mybox" onclick="setemail(\''.$teacher->sts_subject_id.'_'.$teacher->sts_teacher_id.'\',\''.$teacher->sts_subject_id.'\')" id="'.$teacher->sts_subject_id.'_'.$teacher->sts_teacher_id.'" value="'.$email_teacher->email.'">'.'</td></tr>';
                    
                   }
                    else
                    $str_survey=$str_survey.'<tr><td>'.'No Remind'.'</td></tr>';

                } 
                 $str_survey=$str_survey.'</table>';
                 $str_survey=$str_survey.'</form>';
                $str_survey=$str_survey.'</td>';
//
                
                }
                else {
                //Subject
                $subject_id[$i]= $subject->sts_subject_id;
                if($subject_id[$i]!= $subject_id[$i-1])
                {
                $subject_n ='';
                $subject_names = $DB->get_records('subject',array('id'=>$subject_id[$i]));
                
                if($curLang=='en'|| $curLang==NULL){
                foreach($subject_names as $subject_name)
                $subject_n = $subject_name->subject_name_en;
                }
                
                if($curLang=='vi'){
                foreach($subject_names as $subject_name)
                $subject_n = $subject_name->subject_name_vn;
                } 
                
                if($subject_n =='')
                $subject_n = $subject_name->subject_name_vn;
                
                $str_survey=$str_survey.'<tr style="cursor:pointer;"><td>'.$subject_n.'</td>';
                
                //teacher
                $str_survey=$str_survey.'<td>';
                $str_survey=$str_survey.'<table>';
                $teachers = $DB->get_records('itus_teachers_subjects',array('sts_subject_id'=>$subject_id[$i],'sts_survey_id'=>$survey_id));
                $n=0;
                foreach($teachers as $teacher){
                    $teacher_id[$n] = $teacher->sts_teacher_id;
                    
                    $teachername = $DB->get_record('user',array('id'=>$teacher_id[$n]));
                    $teacher_name = $teachername->firstname.' '.$teachername->lastname;
                    $str_survey=$str_survey.'<tr><td>'.$teacher_name.'</td></tr>';
                    
                    $n++;
                }
                $str_survey=$str_survey.'</table>';
                $str_survey=$str_survey.'</td>';
                
                //Status
                $str_survey=$str_survey.'<td>';
                $str_survey=$str_survey.'<table>';
                $teachers = $DB->get_records('itus_teachers_subjects',array('sts_subject_id'=>$subject_id[$i],'sts_survey_id'=>$survey_id));
                foreach($teachers as $teacher){
                    if($teacher->sts_status ==0)
                    $str_survey=$str_survey.'<tr><td>'.get_string('notdo','local_itusurveyplugin').'</td></tr>';
                    
                    if($teacher->sts_status ==1)
                    $str_survey=$str_survey.'<tr><td>'.get_string('doing','local_itusurveyplugin').'</td></tr>';
                    
                    if($teacher->sts_status ==2)
                    $str_survey=$str_survey.'<tr><td>'.get_string('did','local_itusurveyplugin').'</td></tr>';
                }
                    
                 $str_survey=$str_survey.'</table>';
                $str_survey=$str_survey.'</td>'; 

                // Remind vs email
                $str_survey=$str_survey.'<td>';
                
                $str_survey=$str_survey.'<form action="../itusurveyreport/email.php" method="POST">';
                $str_survey=$str_survey.'<table>';
                $teachers = $DB->get_records('itus_teachers_subjects',array('sts_subject_id'=>$subject_id[$i],'sts_survey_id'=>$survey_id));
              foreach($teachers as $teacher){
                    if($teacher->sts_status ==0)
                    {
                    
                    $subject_teacher_string = $subject_teacher_string.','.$teacher->sts_subject_id.'_'.$teacher->sts_teacher_id.',';
                    $email_teacher = $DB->get_record('user',array('id'=>$teacher->sts_teacher_id));
                    $email_teacher_string=$email_teacher_string.','.$email_teacher->email.'//'.$teacher->sts_subject_id.',';
                    $str_survey=$str_survey.'<tr><td>'.'<input type="checkbox" name="mybox" onclick="setemail(\''.$teacher->sts_subject_id.'_'.$teacher->sts_teacher_id.'\',\''.$teacher->sts_subject_id.'\')" id="'.$teacher->sts_subject_id.'_'.$teacher->sts_teacher_id.'" value="'.$email_teacher->email.'">'.'</td></tr>';
                    }
                    else
                    $str_survey=$str_survey.'<tr><td>'.'No Remind'.'</td></tr>';

                } 
                 $str_survey=$str_survey.'</table>';
                 $str_survey=$str_survey.'</form>';
                $str_survey=$str_survey.'</td>';
                }
                }
                $str_survey=$str_survey.'</tr>';
                $i++;
            }

            
            $str_survey=$str_survey.'</table>';

//   //         // buton checkall and uncheckall
////            $str_survey=$str_survey.'<table align="center">';
////            $str_survey=$str_survey.'<tr>';
////            $str_survey=$str_survey.'<td align="center"><input type="button" value="Check all" onclick="checkallemail(\''.$subject_teacher_string.'\',\''.$email_teacher_string.'\')"/></td>';
////            $str_survey=$str_survey.'<td align="center"><input type="button" value="Uncheck all" onclick="uncheckallemail(\''.$subject_teacher_string.'\',\''.$email_teacher_string.'\')"/></td>';
////            $str_survey=$str_survey.'</tr>';
////            $str_survey=$str_survey.'<table>';
            }
            ////////////////////////////////////////////////
            else{
                
                
            //table 3
			$str_survey=$str_survey.'<table class=generalbox border=1 align=center style="margin-left:16%;">'.
			'<th class="headingblock header">'.get_string('subject','local_itusurveyplugin').'</th>'.
			'<th class="headingblock header">'.get_string('teachername','local_itusurveyplugin').'</th>'.
            '<th class="headingblock header">'.get_string('status','local_itusurveyplugin').'</th>'.
            //'<th class="headingblock header">'.get_string('remind','local_itusurveyplugin').'</th>'.
            '<th class="headingblock header">'.get_string('email','local_itusurveyplugin').' '.'<input type="checkbox" id="checkall" onclick="checkallemail($(\'#string_teacher\').val(),$(\'#string_email\').val())"'.'</th>';
            
            $subjects = $DB->get_records('itus_teachers_subjects', array('sts_survey_id'=>$survey_id));
            
            $subject_id = array();
            $teacher_id = array();
            $i=0;
            foreach($subjects as $subject)
            {
                $subject_n='';
                if($i==0){
                //Subject
                $subject_id[$i]= $subject->sts_subject_id;
                $subject_names = $DB->get_records('subject',array('id'=>$subject_id[$i]));
                
                if($curLang=='en'|| $curLang==NULL){
                foreach($subject_names as $subject_name)
                $subject_n = $subject_name->subject_name_en;
                }
                
                if($curLang=='vi'){
                foreach($subject_names as $subject_name)
                $subject_n = $subject_name->subject_name_vn;
                } 
                
                if($subject_n =='')
                $subject_n = $subject_name->subject_name_vn;
                
                //Count Status
                $countstautus = $DB->get_records('itus_teachers_subjects',array('sts_survey_id'=>$survey_id,'sts_subject_id'=>$subject_id[$i]));
                $temp =0;
                foreach($countstautus as $count)
                {
                    if($status_id==1)
                    if($count->sts_status == 0)
                    $temp++;
                    
                    if($status_id==2)
                    if($count->sts_status == 1)
                    $temp++;
                    
                    if($status_id==3)
                    if($count->sts_status == 2)
                    $temp++;
                }
                if($temp !=0 ){
                $str_survey=$str_survey.'<tr style="cursor:pointer;"><td>'.$subject_n.'</td>';
                
                //Teacher
                $str_survey=$str_survey.'<td>';
                $str_survey=$str_survey.'<table>';
                $teachers = $DB->get_records('itus_teachers_subjects',array('sts_subject_id'=>$subject_id[$i],'sts_survey_id'=>$survey_id));
                $n=0;
                foreach($teachers as $teacher){
                     $teacher_id[$n] = $teacher->sts_teacher_id;
                    
                    $teachername = $DB->get_record('user',array('id'=>$teacher_id[$n]));
                    $teacher_name = $teachername->firstname.' '.$teachername->lastname;
                    if($teacher->sts_status == 0 && $status_id==1)
                    $str_survey=$str_survey.'<tr><td>'.$teacher_name.'</td></tr>';
                    if($teacher->sts_status == 1 && $status_id==2)
                    $str_survey=$str_survey.'<tr><td>'.$teacher_name.'</td></tr>';
                    if($teacher->sts_status == 2 && $status_id==3)
                    $str_survey=$str_survey.'<tr><td>'.$teacher_name.'</td></tr>';
                    
                    $n++;
                }
                $str_survey=$str_survey.'</table>';
                $str_survey=$str_survey.'</td>';
                
                
                //Status
                $str_survey=$str_survey.'<td>';
                $str_survey=$str_survey.'<table>';
                $teachers = $DB->get_records('itus_teachers_subjects',array('sts_subject_id'=>$subject_id[$i],'sts_survey_id'=>$survey_id));
                foreach($teachers as $teacher){
                    if($teacher->sts_status ==0 && $status_id ==1)
                    $str_survey=$str_survey.'<tr><td>'.get_string('notdo','local_itusurveyplugin').'</td></tr>';
                    
                    if($teacher->sts_status ==1&& $status_id ==2)
                    $str_survey=$str_survey.'<tr><td>'.get_string('doing','local_itusurveyplugin').'</td></tr>';
                    
                    if($teacher->sts_status ==2&& $status_id ==3)
                    $str_survey=$str_survey.'<tr><td>'.get_string('did','local_itusurveyplugin').'</td></tr>';
                }
                    
                 $str_survey=$str_survey.'</table>';
                $str_survey=$str_survey.'</td>';
                
                // Remind vs email
                $str_survey=$str_survey.'<td>';
                
                $str_survey=$str_survey.'<form action="../itusurveyreport/email.php" method="POST">';
                $str_survey=$str_survey.'<table>';
                $teachers = $DB->get_records('itus_teachers_subjects',array('sts_subject_id'=>$subject_id[$i],'sts_survey_id'=>$survey_id));
              foreach($teachers as $teacher){
                    if($teacher->sts_status ==0 && $status_id ==1)
                    {
                    $subject_teacher_string = $subject_teacher_string.','.$teacher->sts_subject_id.'_'.$teacher->sts_teacher_id.',';
                    $email_teacher = $DB->get_record('user',array('id'=>$teacher->sts_teacher_id));
                    $email_teacher_string=$email_teacher_string.','.$email_teacher->email.'//'.$teacher->sts_subject_id.',';
                    $str_survey=$str_survey.'<tr><td>'.'<input type="checkbox" name="mybox" onclick="setemail(\''.$teacher->sts_subject_id.'_'.$teacher->sts_teacher_id.'\',\''.$teacher->sts_subject_id.'\')" id="'.$teacher->sts_subject_id.'_'.$teacher->sts_teacher_id.'" value="'.$email_teacher->email.'">'.'</td></tr>';
                    }
                } 
                 $str_survey=$str_survey.'</table>';
                 $str_survey=$str_survey.'</form>';
                $str_survey=$str_survey.'</td>';
                }
                }
                else {
                //Subject
                $subject_id[$i]= $subject->sts_subject_id;
                if($subject_id[$i]!= $subject_id[$i-1])
                {
                $subject_n ='';
                $subject_names = $DB->get_records('subject',array('id'=>$subject_id[$i]));
                
                if($curLang=='en'|| $curLang==NULL){
                foreach($subject_names as $subject_name)
                $subject_n = $subject_name->subject_name_en;
                }
                
                if($curLang=='vi'){
                foreach($subject_names as $subject_name)
                $subject_n = $subject_name->subject_name_vn;
                } 
                
                if($subject_n =='')
                $subject_n = $subject_name->subject_name_vn;
                
                //Count Status
                $countstautus = $DB->get_records('itus_teachers_subjects',array('sts_survey_id'=>$survey_id,'sts_subject_id'=>$subject_id[$i]));
                $temp =0;
                foreach($countstautus as $count)
                {
                    if($status_id==1)
                    if($count->sts_status == 0)
                    $temp++;
                    
                    if($status_id==2)
                    if($count->sts_status == 1)
                    $temp++;
                    
                    if($status_id==3)
                    if($count->sts_status == 2)
                    $temp++;
                }
                if($temp !=0 ){
                $str_survey=$str_survey.'<tr style="cursor:pointer;"><td>'.$subject_n.'</td>';
                
                //teacher
                $str_survey=$str_survey.'<td>';
                $str_survey=$str_survey.'<table>';
                $teachers = $DB->get_records('itus_teachers_subjects',array('sts_subject_id'=>$subject_id[$i],'sts_survey_id'=>$survey_id));
                $n=0;
                foreach($teachers as $teacher){
                    $teacher_id[$n] = $teacher->sts_teacher_id;
                    
                    $teachername = $DB->get_record('user',array('id'=>$teacher_id[$n]));
                    $teacher_name = $teachername->firstname.' '.$teachername->lastname;
                    if($teacher->sts_status == 0 && $status_id==1)
                    $str_survey=$str_survey.'<tr><td>'.$teacher_name.'</td></tr>';
                    if($teacher->sts_status == 1 && $status_id==2)
                    $str_survey=$str_survey.'<tr><td>'.$teacher_name.'</td></tr>';
                    if($teacher->sts_status == 2 && $status_id==3)
                    $str_survey=$str_survey.'<tr><td>'.$teacher_name.'</td></tr>';
                    
                    $n++;
                }
                $str_survey=$str_survey.'</table>';
                $str_survey=$str_survey.'</td>';
                
                //Status
                $str_survey=$str_survey.'<td>';
                $str_survey=$str_survey.'<table>';
                $teachers = $DB->get_records('itus_teachers_subjects',array('sts_subject_id'=>$subject_id[$i],'sts_survey_id'=>$survey_id));
                foreach($teachers as $teacher){
                    if($status_id == 1 && $teacher->sts_status ==0)
                    $str_survey=$str_survey.'<tr><td>'.get_string('notdo','local_itusurveyplugin').'</td></tr>';
                    
                    if($status_id ==2&& $teacher->sts_status == 1)
                    $str_survey=$str_survey.'<tr><td>'.get_string('doing','local_itusurveyplugin').'</td></tr>';
                    
                    if($status_id ==3&& $teacher->sts_status == 2)
                    $str_survey=$str_survey.'<tr><td>'.get_string('did','local_itusurveyplugin').'</td></tr>';
                }
                    
                 $str_survey=$str_survey.'</table>';
                $str_survey=$str_survey.'</td>'; 

                // Remind vs email
                $str_survey=$str_survey.'<td>';
                
                $str_survey=$str_survey.'<form action="../itusurveyreport/email.php" method="POST">';
                $str_survey=$str_survey.'<table>';
                $teachers = $DB->get_records('itus_teachers_subjects',array('sts_subject_id'=>$subject_id[$i],'sts_survey_id'=>$survey_id));
              foreach($teachers as $teacher){
                    if($teacher->sts_status ==0 && $status_id ==1)
                    {
                    $subject_teacher_string = $subject_teacher_string.','.$teacher->sts_subject_id.'_'.$teacher->sts_teacher_id.',';
                    $email_teacher = $DB->get_record('user',array('id'=>$teacher->sts_teacher_id));
                    $email_teacher_string=$email_teacher_string.','.$email_teacher->email.'//'.$teacher->sts_subject_id.',';
                    $str_survey=$str_survey.'<tr><td>'.'<input type="checkbox" name="mybox" onclick="setemail(\''.$teacher->sts_subject_id.'_'.$teacher->sts_teacher_id.'\',\''.$teacher->sts_subject_id.'\')" id="'.$teacher->sts_subject_id.'_'.$teacher->sts_teacher_id.'" value="'.$email_teacher->email.'">'.'</td></tr>';
                    }

                } 
                 $str_survey=$str_survey.'</table>';
                 $str_survey=$str_survey.'</form>';
                $str_survey=$str_survey.'</td>';
                }
                }
                }
                $str_survey=$str_survey.'</tr>';
                $i++;
            }

            
            $str_survey=$str_survey.'</table>';
                
           // $str_survey=$str_survey.'<table class=generalbox border=1 align=center style="margin-left:16%;">'.
//			'<th class="headingblock header">'.get_string('subject','local_itusurveyplugin').'</th>'.
//			'<th class="headingblock header">'.get_string('teachername','local_itusurveyplugin').'</th>'.
//            '<th class="headingblock header">'.get_string('status','local_itusurveyplugin').'</th>'.
//            //'<th class="headingblock header">'.get_string('remind','local_itusurveyplugin').'</th>'.
//            '<th class="headingblock header">'.get_string('email','local_itusurveyplugin').'</th>';
//            
//            $subjects = $DB->get_records('itus_teachers_subjects', array('sts_survey_id'=>$survey_id));
//            
//            $subject_id = array();
//            $teacher_id = array();
//            $i=0;
//            foreach($subjects as $subject)
//            {
//                $subject_n='';
//                
//                if($i==0){
//                //Subject
//                $subject_id[$i]= $subject->sts_subject_id;
//                $subject_names = $DB->get_records('subject',array('id'=>$subject_id[$i]));
//                
//                if($curLang=='en'|| $curLang==NULL){
//                foreach($subject_names as $subject_name)
//                $subject_n = $subject_name->subject_name_en;
//                }
//                
//                if($curLang=='vi'){
//                foreach($subject_names as $subject_name)
//                $subject_n = $subject_name->subject_name_vn;
//                } 
//                
//                //Count Status
//                $countstautus = $DB->get_records('itus_teachers_subjects',array('sts_survey_id'=>$survey_id,'sts_subject_id'=>$subject_id[$i]));
//                $temp =0;
//                foreach($countstautus as $count)
//                {
//                    if($status_id==1)
//                    if($count->sts_status == 0)
//                    $temp++;
//                    
//                    if($status_id==2)
//                    if($count->sts_status == 1)
//                    $temp++;
//                    
//                    if($status_id==3)
//                    if($count->sts_status == 2)
//                    $temp++;
//                }
//                if($temp !=0 ){
//                $str_survey=$str_survey.'<tr style="cursor:pointer;"><td>'.$subject_n.'</td>';
//                
//                //Teacher
//                $str_survey=$str_survey.'<td>';
//                $str_survey=$str_survey.'<table>';
//                $teachers = $DB->get_records('itus_teachers_subjects',array('sts_subject_id'=>$subject_id[$i],'sts_survey_id'=>$survey_id));
//                $n=0;
//                foreach($teachers as $teacher){
//                    $teacher_id[$n] = $teacher->sts_teacher_id;
//                    
//                    $teachername = $DB->get_record('user',array('id'=>$teacher_id[$n]));
//                    $teacher_name = $teachername->firstname.' '.$teachername->lastname;
//                    if($teacher->sts_status == 0 && $status_id==1)
//                    $str_survey=$str_survey.'<tr><td>'.$teacher_name.'</td></tr>';
//                    if($teacher->sts_status == 1 && $status_id==2)
//                    $str_survey=$str_survey.'<tr><td>'.$teacher_name.'</td></tr>';
//                    if($teacher->sts_status == 2 && $status_id==3)
//                    $str_survey=$str_survey.'<tr><td>'.$teacher_name.'</td></tr>';
//                    
//                    $n++;
//                }
//                $str_survey=$str_survey.'</table>';
//                $str_survey=$str_survey.'</td>';
//                
//                
//                //Status
//                $str_survey=$str_survey.'<td>';
//                $str_survey=$str_survey.'<table>';
//                $teachers = $DB->get_records('itus_teachers_subjects',array('sts_subject_id'=>$subject_id[$i],'sts_survey_id'=>$survey_id));
//                foreach($teachers as $teacher){
//                    if($status_id == 1 && $teacher->sts_status ==0)
//                    $str_survey=$str_survey.'<tr><td>'.get_string('notdo','local_itusurveyplugin').'</td></tr>';
//                    
//                    if($status_id ==2&& $teacher->sts_status == 1)
//                    $str_survey=$str_survey.'<tr><td>'.get_string('doing','local_itusurveyplugin').'</td></tr>';
//                    
//                    if($status_id ==3&& $teacher->sts_status == 2)
//                    $str_survey=$str_survey.'<tr><td>'.get_string('did','local_itusurveyplugin').'</td></tr>';
//                }
//                    
//                 $str_survey=$str_survey.'</table>';
//                $str_survey=$str_survey.'</td>';
//                
//                // Remind
//                $str_survey=$str_survey.'<td>';
//                
//                $str_survey=$str_survey.'<form action="../itusurveyreport/email.php" method="POST">';
//                $str_survey=$str_survey.'<table>';
//                $teachers = $DB->get_records('itus_teachers_subjects',array('sts_subject_id'=>$subject_id[$i],'sts_survey_id'=>$survey_id));
//                foreach($teachers as $teacher){
//                    if($status_id == 1 && $teacher->sts_status == 0){
//                    $email_teacher = $DB->get_record('user',array('id'=>$teacher->sts_teacher_id));
//                    $str_survey=$str_survey.'<tr><td>'.'<input type="checkbox" name="mybox" onclick="setemail(\''.$teacher->sts_subject_id.'_'.$teacher->sts_teacher_id.'\',\''.$teacher->sts_subject_id.'\')" id="'.$teacher->sts_subject_id.'_'.$teacher->sts_teacher_id.'" value="'.$email_teacher->email.'">'.'</td></tr>';
//                    
//                    }
//                    else
//                    {
//                        $str_survey=$str_survey.'<tr><td>No email</td></tr>';
//                    }
//                    
//                $str_survey=$str_survey.'</table>';
//                $str_survey=$str_survey.'</form>';
//                $str_survey=$str_survey.'</td>';
//
//                $str_survey=$str_survey.'</tr>';
//                }
//                }
//                else {
//                //Subject
//                $subject_id[$i]= $subject->sts_subject_id;
//                if($subject_id[$i]!= $subject_id[$i-1])
//                {
//                $subject_n ='';
//                $subject_names = $DB->get_records('subject',array('id'=>$subject_id[$i]));
//                
//                if($curLang=='en'|| $curLang==NULL){
//                foreach($subject_names as $subject_name)
//                $subject_n = $subject_name->subject_name_en;
//                }
//                
//                if($curLang=='vi'){
//                foreach($subject_names as $subject_name)
//                $subject_n = $subject_name->subject_name_vn;
//                } 
//                
//                if($subject_n =='')
//                $subject_n = $subject_name->subject_name_vn;
//                
//                //count status
//                $countstautus = $DB->get_records('itus_teachers_subjects',array('sts_survey_id'=>$survey_id,'sts_subject_id'=>$subject_id[$i]));
//                $temp =0;
//                foreach($countstautus as $count)
//                {
//                    if($status_id==1)
//                    if($count->sts_status == 0)
//                    $temp++;
//                    
//                    if($status_id==2)
//                    if($count->sts_status == 1)
//                    $temp++;
//                    
//                    if($status_id==3)
//                    if($count->sts_status == 2)
//                    $temp++;
//                }
//                if($temp !=0 ){
//                $str_survey=$str_survey.'<tr style="cursor:pointer;"><td>'.$subject_n.'</td>';
//                
//                //teacher
//                $str_survey=$str_survey.'<td>';
//                $str_survey=$str_survey.'<table>';
//                $teachers = $DB->get_records('itus_teachers_subjects',array('sts_subject_id'=>$subject_id[$i],'sts_survey_id'=>$survey_id));
//                $n=0;
//                foreach($teachers as $teacher){
//                    $teacher_id[$n] = $teacher->sts_teacher_id;
//                    
//                    $teachername = $DB->get_record('user',array('id'=>$teacher_id[$n]));
//                    $teacher_name = $teachername->firstname.' '.$teachername->lastname;
//                    if($teacher->sts_status == 0 && $status_id==1)
//                    $str_survey=$str_survey.'<tr><td>'.$teacher_name.'</td></tr>';
//                    if($teacher->sts_status == 1 && $status_id==2)
//                    $str_survey=$str_survey.'<tr><td>'.$teacher_name.'</td></tr>';
//                    if($teacher->sts_status == 2 && $status_id==3)
//                    $str_survey=$str_survey.'<tr><td>'.$teacher_name.'</td></tr>';
//                    
//                    $n++;
//                }
//                $str_survey=$str_survey.'</table>';
//                $str_survey=$str_survey.'</td>';
//                
//                //Status
//                $str_survey=$str_survey.'<td>';
//                $str_survey=$str_survey.'<table>';
//                $teachers = $DB->get_records('itus_teachers_subjects',array('sts_subject_id'=>$subject_id[$i],'sts_survey_id'=>$survey_id));
//                foreach($teachers as $teacher){
//                    
//                    if($status_id == 1 && $teacher->sts_status == 0)
//                    $str_survey=$str_survey.'<tr><td>'.get_string('notdo','local_itusurveyplugin').'</td></tr>';
//                    
//                    if($status_id ==2&& $teacher->sts_status == 1)
//                    $str_survey=$str_survey.'<tr><td>'.get_string('doing','local_itusurveyplugin').'</td></tr>';
//                    
//                    if($status_id ==3&& $teacher->sts_status == 2)
//                    $str_survey=$str_survey.'<tr><td>'.get_string('did','local_itusurveyplugin').'</td></tr>';
//                }
//                    
//                 $str_survey=$str_survey.'</table>';
//                $str_survey=$str_survey.'</td>'; 
//                
//                // Remind
//                $str_survey=$str_survey.'<td>';
//                $str_survey=$str_survey.'<table>';
//                $teachers = $DB->get_records('itus_teachers_subjects',array('sts_subject_id'=>$subject_id[$i],'sts_survey_id'=>$survey_id));
//                foreach($teachers as $teacher){
//                    if($teacher->sts_status ==0 && $status_id==1)
//                    {
//                   $email_teacher = $DB->get_record('user',array('id'=>$teacher->sts_teacher_id));
//                   $str_survey=$str_survey.'<tr><td>'.'<input type="checkbox" name="mybox" onclick="setemail(\''.$teacher->sts_subject_id.'_'.$teacher->sts_teacher_id.'\',\''.$teacher->sts_subject_id.'\')" id="'.$teacher->sts_subject_id.'_'.$teacher->sts_teacher_id.'" value="'.$email_teacher->email.'">'.'</td></tr>';
//                    }
//                    else
//                    {
//                        $str_survey=$str_survey.'<tr><td>No email</td></tr>';
//                    }
//                    
//                 $str_survey=$str_survey.'</table>';
//                $str_survey=$str_survey.'</td>';
//
//                }
//                //$str_survey=$str_survey.'</td></tr>';
//                //$str_survey=$str_survey.'</table>';
//                //$str_survey=$str_survey.'</td>';
//                
//                $str_survey=$str_survey.'</tr>';
//                }
//                }
//                }
//                $i++;
//            
//            }
//            }
//            
//            $str_survey=$str_survey.'</table>';
            }
            $str_survey=$str_survey.'<input type="hidden" id="string_teacher" value="'.$subject_teacher_string.'">';
            $str_survey=$str_survey.'<input type="hidden" id="string_email" value="'.$email_teacher_string.'">';
            echo $str_survey;

}
else if ($tim ='showemailteacher')
{
    
    $mylib = new statistic_management();
    
    $subjectStr = $_POST['subject'];
    $contentStr = $_POST['content'];
    $str_email ='';
    $email_array = array();
    $subject_array = array();
    $email_array = explode(',,',$emailstring);
    
    $n = count($email_array);
    $temp = array();
    
    for($i=0;$i<$n;$i++)
    {
        $email_array[$i] = str_replace(",","",$email_array[$i]);
        //$email_array[$i] = $email_array[$i].replace(',','');
        
        $temp = explode('//',$email_array[$i]);
        $email_array[$i] = $temp[0];
        
        $subject = $DB->get_record('subject',array('id'=>$temp[1]));
        $subject_array[$i]= $subject->subject_name_vn;
        
        //$str_email = $str_email.'Subject:'.$subject_array[$i].'</br>';
        $str_email .= $str_email.$email_array[$i].';';
    }
    $from = "itec.moodle@gmail.com";
    $fromName = "ITEC Moodle System";
    $subject = $subjectStr;
    $content = $contentStr;
    $resultSendEmail = $mylib->send_email_notification($from, $fromName, $str_email, $subject, $content);
    if($resultSendEmail){
        echo "Email Sent";
    }else{
        echo "Sending Email Failure, Please try again later";
    }
}


?>